package com.example.wanandroid.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.wanandroid.R;
import com.example.wanandroid.entity.SearchBean;

import java.util.List;
import java.util.Random;

public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.ViewHolder> {

    private Context mContext;
    private LayoutInflater inflater;
    private List<SearchBean> mList;

    private NavigationAdapter.OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(NavigationAdapter.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public SearchAdapter(Context mContext, List<SearchBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.item_search, null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
        SearchBean bean = mList.get(i);
        viewHolder.mTextView.setText(bean.getName());
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onClick(i);
            }
        });

        int min = 1;
        int max = 13;
        Random random = new Random();
        int num = random.nextInt(max) % (max - min + 1) + min;

        switch (num) {
            case 1:
                viewHolder.mTextView.setBackgroundResource(R.drawable.text_bg_blue);
                break;
            case 2:
                viewHolder.mTextView.setBackgroundResource(R.drawable.text_bg_pink);
                break;
            case 3:
                viewHolder.mTextView.setBackgroundResource(R.drawable.text_bg_red);
                break;
            case 4:
                viewHolder.mTextView.setBackgroundResource(R.drawable.text_bg_black_pearl);
                break;
            case 5:
                viewHolder.mTextView.setBackgroundResource(R.drawable.text_bg_fresh_turquoise);
                break;
            case 6:
                viewHolder.mTextView.setBackgroundResource(R.drawable.text_bg_green_teal);
                break;
            case 7:
                viewHolder.mTextView.setBackgroundResource(R.drawable.text_bg_jade_dust);
                break;
            case 8:
                viewHolder.mTextView.setBackgroundResource(R.drawable.text_bg_london_square);
                break;
            case 9:
                viewHolder.mTextView.setBackgroundResource(R.drawable.text_bg_megaman);
                break;
            case 10:
                viewHolder.mTextView.setBackgroundResource(R.drawable.text_bg_minty_green);
                break;
            case 11:
                viewHolder.mTextView.setBackgroundResource(R.drawable.text_bg_periwinkle);
                break;
            case 12:
                viewHolder.mTextView.setBackgroundResource(R.drawable.text_bg_spiro_disco_ball);
                break;
            case 13:
                viewHolder.mTextView.setBackgroundResource(R.drawable.text_bg_yellow);
                break;
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView mTextView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mTextView = itemView.findViewById(R.id.mTextView);
            int width = ((Activity) mTextView.getContext()).getWindowManager().getDefaultDisplay().getWidth();
            ViewGroup.LayoutParams params = mTextView.getLayoutParams();
            //设置图片的相对于屏幕的宽高比
            params.width = /*width/2*/ (int) (200 + Math.random() * 400);
            //params.height =  (int) (200 + Math.random() * 400) ;
            mTextView.setLayoutParams(params);
        }
    }

    public interface OnItemClickListener {
        void onClick(int position);
    }
}
